# 导包
import requests
import config


class OrderAPI:
    # 1,登录
    def login(self, username, password):
        url = config.BASE_URL + "/wx/auth/login"
        login_data = {
            "username": username,
            "password": password
        }
        response = requests.post(url=url, json=login_data)
        return response

    # 2,搜索
    def search(self, keyword):
        return requests.get(url=config.BASE_URL + f"/wx/goods/list?keyword={keyword}&page=1&limit=10&categoryId=0")

    # 3,加入购物车
    def add_cart(self, token, add_data):
        return requests.post(url=config.BASE_URL + f"/wx/cart/add",
                             json=add_data,
                             headers={"X-Litemall-Token": token})

    # 4,提交订单
    def submit_order(self, token, test_data):
        return requests.post(url=config.BASE_URL + f"/wx/order/submit",
                             json=test_data,
                             headers={"X-Litemall-Token": token})


if __name__ == '__main__':
    # 登录
    order_api = OrderAPI()
    response = order_api.login(username="zj010128", password="a123456")
    # 提取token
    token = response.json()["data"]["token"]
    print(token)
    print(response.json())
    # 搜索
    response = order_api.search("母亲节")
    # 提取商品id
    goods_id = response.json()["data"]["list"][0]["id"]
    # print(response.json())
    print(goods_id)
    # 加入购物车
    add_data = {
        "goodsId": goods_id,
        "number": 5,
        "productId": 2
    }
    response = order_api.add_cart(token, add_data)
    print(response.json())
    # 提交订单
    test_data = {"addressId": "2", "cartId": "1", "couponId": "0", "user_couponId": "0", "grouponLinkId": 0,
                 "grouponRulesId": 0,
                 "message": ""}
    response = order_api.submit_order(token, test_data)
    print(response.json())
